//
//  DebugViewHelper.swift
//  test-ios
//
//  Created by wyz5204 on 2023/6/4.
//

import Foundation
import UIKit
import RxSwift
import SnapKit
import PopMenu

class DebugViewHelper : UIButton {
    
    struct Callback{
        let name : String
        let call : ()->Void
    }
    
    private var debug  = [Callback]()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        initView()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        initView()
    }
    
    func setDebug(_ action : Callback...) -> DebugViewHelper{
        self.debug.append(contentsOf: action)
        return self
    }
    
    private func initView(){
        setTitleAndColor("Debug")
        rx.tap.asDriver().drive(onNext: { [weak self] in
            guard let this = self else {
                return
            }
            guard this.debug.count > 0 else {
                return
            }
            guard let superView = this.superview else{
                return
            }
            var actions : [PopMenuAction] = []
            for (value) in this.debug {
                actions.append(PopMenuDefaultAction(title: value.name , didSelect: {action in
                    value.call()
                }))
            }
            let menuViewController = PopMenuViewController(sourceView : this , actions: actions)
            // 传入一个 UIView
            this.currentViewController().present(menuViewController, animated: true, completion: nil)
        }, onDisposed: {
            
            
        }).disposed(by: rx.disposeBag)
    }
    
}


